<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/15
 * Time: 16:13
 */

namespace app\index\ui;

class BuildForm {

    static public function buildForm($config,$data){
        $formStr = '';
        foreach ($config as $con){
            $con['VALUE'] = isset($data[$con['FIELD']])?$data[$con['FIELD']]:'';

            if($con['TYPE'] == 'text'){
                $formStr .= self::text($con);
            }
            else if($con['TYPE'] == 'hidden'){
                $formStr .= self::hidden($con);
            }
            else if($con['TYPE'] == 'line'){
                $formStr .= self::line();
            }
            else if($con['TYPE'] == 'date'){
                $formStr .= self::date($con);
            }
            else if($con['TYPE'] == 'select'){
                $formStr .= self::select($con);
            }
            else if($con['TYPE'] == 'selectYear'){
                $formStr .= self::selectYear($con);
            }
        }
        return $formStr;
    }

    static public function line(){
        $html = <<<EOF
    <div class="col-sm-12" style="border-bottom: 1px solid #f1f1f1;margin-top: 6px;margin-bottom: 6px;"></div>
EOF;
        return $html;
    }

    static public function hidden($config){
        $name = isset($config['NAME'])?$config['NAME']:'';
        $value = isset($config['VALUE'])?$config['VALUE']:'';
        $html = <<<EOF
            <input name="$name" hidden value="$value">
EOF;
        return $html;
    }

    static public function text($config){
        $title = isset($config['TITLE'])?$config['TITLE']:'';
        $name = isset($config['NAME'])?$config['NAME']:'';
        $value = isset($config['VALUE'])?$config['VALUE']:'';
        $mist = isset($config['MIST'])?$config['MIST']:'';
        $col = isset($config['col'])?$config['col']:'col-sm-6 col-md-6 col-lg-4';
        $disable = isset($config['DISABLED'])?$config['DISABLED']:'';
        if($disable == '1'){
            $disable = 'disabled';
        }
        $hidden = isset($config['hidden'])?$config['hidden']:'';
        $readonly = isset($config['readonly'])?$config['readonly']:'';
        $pl = isset($config['pl'])?$config['pl']:'';


        $html = <<<EOF
                    <div class="$col" style="padding-top:4px;padding-bottom: 4px;">
                        <div class="input-group" style="width: 100%;">
                            <span class="input-group-addon" style="width: 100px;text-align: right">
                                <span style="color:red">$mist</span>
                                $title
                            </span>
                            <input name="$name" $disable $hidden $readonly value="$value" placeholder="$pl" type="text" class="form-control">
                        </div>
                    </div>
EOF;
        return $html;
    }

    static public function date($config){
        $title = isset($config['TITLE'])?$config['TITLE']:'';
        $name = isset($config['NAME'])?$config['NAME']:'';
        $value = isset($config['VALUE'])?$config['VALUE']:'';
        $mist = isset($config['MIST'])?$config['MIST']:'';
        $col = isset($config['col'])?$config['col']:'col-sm-6 col-md-6 col-lg-4';
        $pl = isset($config['pl'])?$config['pl']:'';

        $html = <<<EOF
                    <div class="$col" style="padding-top:4px;padding-bottom: 4px;">
                        <div class="input-group" style="width: 100%;">
                            <span class="input-group-addon" style="width: 100px;text-align: right">
                                <span style="color:red">$mist</span>
                                $title
                            </span>
                            <input name="$name" value="$value" placeholder="$pl" type="text" style="background-color: white" class="form-control">
                        </div>
                    </div>
<script>
$(function() {
    $("input[name='$name']").flatpickr();
});
</script>
EOF;
        return $html;
    }

    static public function select($config){

        $title = isset($config['TITLE'])?$config['TITLE']:'';
        $name = isset($config['NAME'])?$config['NAME']:'';
        $value = isset($config['VALUE'])?$config['VALUE']:'';
        $s_url = isset($config['S_URL'])?$config['S_URL']:'';
        $url = url($s_url);
        $s_key = isset($config['S_KEY'])?$config['S_KEY']:'ID';
        $s_field = isset($config['S_FIELD'])?$config['S_FIELD']:'NAME';
        $mist = isset($config['mist'])?$config['mist']:'';
        $col = isset($config['col'])?$config['col']:'col-sm-6 col-md-6 col-lg-4';
        $disable = isset($config['DISABLED'])?$config['DISABLED']:'';
        if($disable == '1'){
            $disable = 'disabled';
        }

        $options = "<option value='' style='display: none'>请选择...</option>";

        $html = <<<EOF
                <div class="$col" style="padding-top: 4px;padding-bottom: 4px;">
                    <div class="input-group" style="width: 100%;">
                        <span class="input-group-addon" style="width: 100px;text-align: right"><span style="color:red">$mist</span>$title</span>
                        <select name="$name" $disable class="form-control">
                        $options
                        </select>
                    </div>
                </div>
<script>
    $(function() {
        $.ajax({
            url:"$url",
            type:'post',
            success:function(data) {
                var isSelect = false;
                console.log(data);
                for(var i = 0 ; i < data.length ; i++){
                    var option = "<option value='" + data[i]["$s_key"] + "'>"
                    + data[i]["$s_key"] + ":" + data[i]["$s_field"] + "</option>";
                    $("[name='$name']").append(option);
                    if("$value" == data[i]["$s_key"]){
                        isSelect = true;    
                    }
                }
                if(isSelect){
                    $("[name='$name']").val("$value");
                }
            },
            error:function(res) {
                layer.alert("获取[$title]数据失败",{icon:2});
            }
        });
    });
</script>
EOF;
        return $html;
    }

    static public function selectYear($config){

        $title = isset($config['TITLE'])?$config['TITLE']:'';
        $name = isset($config['NAME'])?$config['NAME']:'';
        $value = isset($config['VALUE'])?$config['VALUE']:'';
        $s_url = isset($config['S_URL'])?$config['S_URL']:'';
        $url = url($s_url);
//        $s_key = isset($config['S_KEY'])?$config['S_KEY']:'ID';
//        $s_field = isset($config['S_FIELD'])?$config['S_FIELD']:'NAME';
        $mist = isset($config['mist'])?$config['mist']:'';
        $col = isset($config['col'])?$config['col']:'col-sm-6 col-md-6 col-lg-4';
        $disable = isset($config['DISABLED'])?$config['DISABLED']:'';
        if($disable == '1'){
            $disable = 'disabled';
        }

        $options = "<option value='' style='display: none'>请选择...</option>";

        $html = <<<EOF
                <div class="$col" style="padding-top: 4px;padding-bottom: 4px;">
                    <div class="input-group" style="width: 100%;">
                        <span class="input-group-addon" style="width: 100px;text-align: right"><span style="color:red">$mist</span>$title</span>
                        <select name="$name" $disable class="form-control">
                        $options
                        </select>
                    </div>
                </div>
<script>
    $(function() {
        $.ajax({
            url:"$url",
            type:'post',
            success:function(data) {
                var isSelect = false;
                // console.log(data);
                for(var i = 0 ; i < data.length ; i++){
                    var option = "<option value='" + data[i] + "'>"
                    + data[i] + "</option>";
                    $("[name='$name']").append(option);
                    if("$value" == data[i]){
                        isSelect = true;    
                    }
                }
                if(isSelect){
                    $("[name='$name']").val("$value");
                }
            },
            error:function(res) {
                layer.alert("获取[$title]数据失败",{icon:2});
            }
        });
    });
</script>
EOF;
        return $html;
    }

}